﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;

namespace SharpSoft.WPF.Validations
{
    /// <summary>
    /// 值验证规则
    /// </summary>
    public class ValueValidationRule : ValidationRule
    {
        public override ValidationResult Validate(object value, CultureInfo cultureInfo)
        {
            if (value == null)
            {
                if (!AllowNull)
                {
                    return new ValidationResult(false, "必须填写");
                }
                else
                {
                    return ValidationResult.ValidResult;
                }
            }
            return Uncertainty;//不确定的结果
        }
        /// <summary>
        /// 不确定的验证结果，表示需要继续验证其他规则。
        /// </summary>
        public static ValidationResult Uncertainty { get; private set; } = new ValidationResult(true, "Uncertainty");

        /// <summary>
        /// 是否允许值为null
        /// </summary>
        public bool AllowNull { get; set; }
        /// <summary>
        /// 为null时的错误消息
        /// </summary>
        public string NullError { get; set; } = "必须填写";
    }
}
